package com.hsnn.medstgmini.trading.pur.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.trading.pur.dao.PurConfigDao;
import com.hsnn.medstgmini.trading.pur.model.PurConfig;

/**
 * @category 采购设置
 * @author 蔡春龙
 * @date 2015年6月14日
 */
@Repository
public class PurConfigDaoImpl implements PurConfigDao {

	@Resource(name = "sessionFactory")
	private SessionFactory factory;
	@Value("#{sysProp['system.province']}")
    private String sysProvince;
	@Autowired
	private HibernateSqlUtil dao;
	
	private static final Logger log = Logger.getLogger(PurConfigDaoImpl.class);

	/**
	 * @category 查询采购设置列表
	 * @date 2015年7月7日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<PurConfig> getPurConfigList() {
		Session session = factory.getCurrentSession();
		Criteria criteria = session.createCriteria(PurConfig.class);
		return criteria.list();
	}
	/**
	 * @category 查询采购设置列表
	 * @date 2015年11月12日
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<PurConfig> getPurConfigLists() {
		Session session = factory.getCurrentSession();
		StringBuffer sql = new StringBuffer();
		sql.append(" select ");
		sql.append(" config_id configId, config_no configNo, config_opt configOpt,"); 
		sql.append(" config_value configValue, config_remark configRemark,");
		sql.append(" config_politic configPolitic, area_id areaId ,check_rule checkRule");
		sql.append(" from sys_purconfig ");
		sql.append(" where area_id= :areaId "); 
		sql.append(" order by config_no asc ");
		Query query = session.createSQLQuery(sql.toString());
		query.setString("areaId", sysProvince);
		query.setResultTransformer(Transformers.aliasToBean(PurConfig.class));
		return query.list();
		
	}

	/**
	 * @category 保存采购设置
	 * @date 2015年7月7日
	 * @param config
	 */
	@SuppressWarnings("unchecked")
	public void savePurConfig(List<PurConfig> list) {
		Session session = factory.getCurrentSession();
		if (list != null && !list.isEmpty()) {
			for (PurConfig config : list) {
				Criteria criteria = session.createCriteria(PurConfig.class);
				criteria.add(Restrictions.eq("configId", config.getConfigId()));
				List<PurConfig> configList = criteria.list();
				if (configList != null && !configList.isEmpty()) {
					PurConfig old = configList.get(0);
					if (old != null) {
						old.setConfigValue(config.getConfigValue());
						session.update(old);
					} else {
						log.warn("没有找到要保存的设置项！");
					}
				}
			}
			session.flush();
		}
	}

	/**
	 * @category 根据Id查询采购设置信息
	 * @author 韩守松
	 * @date   2015年7月31日
	 * @param  @param Id
	 * @param  @return
	 */
	public PurConfig getPurConfigById(String Id) {
		Session session = factory.getCurrentSession();
		StringBuffer hql = new StringBuffer();
		hql.append(" from PurConfig pc where pc.configId = :configId ");
		Query query = session.createQuery(hql.toString());
		query.setString("configId", Id);
		return (PurConfig)query.setMaxResults(1).uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getBaseDelAreaByCode(String areaCode, String delCompCode){
		String sql = "select * from base_del_area_rel a where a.area_code = :areaCode and a.del_comp_code = :delCompCode";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("areaCode", areaCode);
		params.put("delCompCode", delCompCode);
		return (List<Map<String, Object>>) dao.getSqlMapList(sql, params);
	}
}
